﻿// Study0802.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 第八章 多态 重载运算符 成员函数 单目运算符

#include <iostream>
using namespace std;

class clock
{
public:
	clock(int hh, int mm, int ss) 
	{
		if (0 <= hh && hh <= 24 && 0 <= mm && mm <= 60 && 0 <= ss && ss<= 60)
		{
			hour = hh;
			minute = mm;
			second = ss;
		}
		else
		{
			cout << "Time error!" << endl;
		}
	}
	clock() :clock(0, 0, 0) {}
	const clock& operator ++() //前置++
	{
		this->second++;
		if (second >= 60)
		{
			second %= 60;
			minute++;
			if (minute >= 60)
			{
				minute %= 60;
				hour=(++hour)%24;
				
			}
		}
		return *this;
	}
	clock  operator ++(int) //后置++
	{
		const clock old = *this;
		++(*this);
		return old;
	}
	void showTime() const
	{
		cout << hour << ":" << minute << ":" << second << endl;
	}
private:
	int hour, minute, second;
};

int main()
{
	class clock clock(23, 59, 59);
	cout << "first clock output" << endl;
	clock.showTime();
	cout << "Show myclock++: ";
	(clock++).showTime();
	cout << "Show ++myclock: ";
	(++clock).showTime();
	return 0;
}


